package com.lecture.springlecture.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.minio.BucketExistsArgs;
import io.minio.MinioClient;
import io.minio.StatObjectArgs;
import io.minio.StatObjectResponse;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

/**
 * @author luyn
 * @version 1.0
 * @description
 * @date 2022/9/7 14:24
 */
@RestController
@RequestMapping("http")
public class HttpController {

    @RequestMapping("/test")
    public String test(HttpServletRequest request) {
        //获取IP地址
        String ipAddress = getIpAddr(request);
        return ipAddress;
    }

    public static String getIpAddr(HttpServletRequest request)
    {
        if (request == null)
        {
            return "unknown";
        }
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Forwarded-For");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getHeader("X-Real-IP");
        }

        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip))
        {
            ip = request.getRemoteAddr();
        }

        return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : getMultistageReverseProxyIp(ip);
    }
    /**
     * 从多级反向代理中获得第一个非unknown IP地址
     *
     * @param ip 获得的IP地址
     * @return 第一个非unknown IP地址
     */
    public static String getMultistageReverseProxyIp(String ip)
    {
        // 多级反向代理检测
        if (ip != null && ip.indexOf(",") > 0)
        {
            final String[] ips = ip.trim().split(",");
            for (String subIp : ips)
            {
                if (false == isUnknown(subIp))
                {
                    ip = subIp;
                    break;
                }
            }
        }
        return ip;
    }
    /**
     * 检测给定字符串是否为未知，多用于检测HTTP请求相关
     *
     * @param checkString 被检测的字符串
     * @return 是否未知
     */
    public static boolean isUnknown(String checkString)
    {
        return StringUtils.isBlank(checkString) || "unknown".equalsIgnoreCase(checkString);
    }

    public static void main(String[] args) {
        MinioClient minioClient = MinioClient.builder().endpoint("http://127.0.0.1:9000").credentials("minioadmin","minioadmin").build();
        System.out.println(minioClient);
        try {
            System.out.println(minioClient.bucketExists(BucketExistsArgs.builder().bucket("data1").build()));
            StatObjectResponse data1 = minioClient.statObject(StatObjectArgs.builder().bucket("data1").object("date1/1ic.png").build());
            System.out.println(data1.lastModified());
            ZonedDateTime create = data1.lastModified();
            LocalDateTime localDateTime = create.toLocalDateTime();
            System.out.println(localDateTime);
            create = create.withZoneSameInstant(ZoneId.of("Asia/Shanghai"));
            System.out.println(create);
            //minioClient.removeObject(RemoveObjectArgs.builder().bucket("data1").object("/tmp/1270260.jpg").build());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
